package com.help10000.rms.ui.fragments

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.Html
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.amir.common.beans.HomeData
import com.amir.common.utils.SPUtils
import com.amir.common.weight.DuplicateClickProxy
import com.help10000.rms.R
import com.help10000.rms.common.OrderType
import com.help10000.rms.config.PERMISSION_URL
import com.help10000.rms.mvp.presenter.WorkOrderPresenter
import com.help10000.rms.mvp.view.WorkOrderView
import com.help10000.rms.ui.activitys.OpenUrlActivity
import com.help10000.rms.ui.activitys.OrderActivity
import com.help10000.rms.ui.activitys.mine.msg.MessageCenterActivity
import com.help10000.rms.ui.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_work_order.*


class WorkOrderFragment : BaseFragment<WorkOrderPresenter, WorkOrderView>(),
    View.OnClickListener, WorkOrderView, SwipeRefreshLayout.OnRefreshListener {

    var versionCode = 0;

    companion object {
        @JvmStatic
        fun newInstance() =
            WorkOrderFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }

    override fun getlayoutId(): Int {
        return R.layout.fragment_work_order
    }

    override fun initView(rootView: View?) {
        //上面的方法已经废弃
        swiperefresh.setColorSchemeColors(
            resources.getColor(R.color.colorPrimary),
            resources.getColor(R.color.colorPrimary),
            resources.getColor(R.color.colorPrimary),
            resources.getColor(R.color.colorPrimary)
        )

        versionCode = requireActivity().packageManager!!.getPackageInfo(
            getViewContext()!!.packageName, 0
        ).versionCode
        // 判断api版本号是否大于等于21
        if (Build.VERSION.SDK_INT > 23) {
            var params = rl_boobar.layoutParams as (LinearLayout.LayoutParams)
            params.topMargin = getStatusBarHeight()
            rl_boobar.layoutParams = params;
        }
        msgTipTextView.text = Html.fromHtml(activity?.getString(R.string.msg_tip))

        var flag = SPUtils.getInt("msg_flag_$versionCode");
        if (flag != 1)
            msgTipLayout.visibility = View.VISIBLE
    }

    fun getStatusBarHeight(): Int {
        var result = 0
        //获取状态栏高度的资源id
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = resources.getDimensionPixelSize(resourceId)
        }
        return result
    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    override fun showLoading() {

    }

    override fun onResume() {
        super.onResume()
        mPresenter.getData()
    }

    override fun lazyLoad() {

    }

    override fun initListener() {
        ll_order.setOnClickListener(DuplicateClickProxy(this))
        ll_rborder.setOnClickListener(DuplicateClickProxy(this))
        ll_overtime.setOnClickListener(DuplicateClickProxy(this))
        swiperefresh.setOnRefreshListener(this)
        closeMsgTip.setOnClickListener(DuplicateClickProxy(this))
        msgTipTextView.setOnClickListener(DuplicateClickProxy(this))
        appMessageImageView.setOnClickListener(DuplicateClickProxy(this))
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ll_order -> {//我的订单
                startActivity(Intent(mContext, OrderActivity::class.java))
            }
            R.id.ll_rborder -> {//抢单
                var intent = Intent(mContext, OrderActivity::class.java)
                intent.putExtra("type", OrderType.ROB_ORDER.ordinal)
                startActivity(intent)
            }
            R.id.ll_overtime -> {//超时订单
                var intent = Intent(mContext, OrderActivity::class.java)
                intent.putExtra("type", OrderType.TIMEOUT_ORDER.ordinal)
                startActivity(intent)
            }
            R.id.appMessageImageView -> {
                startActivity(Intent(mContext, MessageCenterActivity::class.java))
            }
            R.id.closeMsgTip -> {
                SPUtils.save("msg_flag_$versionCode", 1)
                (msgTipLayout.parent as ViewGroup).removeView(msgTipLayout)
            }
            R.id.msgTipTextView -> {
                OpenUrlActivity.startActivity(
                    requireContext(),
                    PERMISSION_URL + "?brand=" + Build.BRAND + "&model=" + Build.MODEL + "&version=" + Build.VERSION.RELEASE
                )
            }
        }
    }

    override fun dismissLoading() {
        super.dismissLoading()
        swiperefresh.isRefreshing = false
    }

    override fun SuccessData(data: HomeData) {
        swiperefresh.isRefreshing = false
        if (true) {//data.Review.CanView
            tv_todayorder.text = data.Review.Qty1
            tv_notAssQty.text = data.Review.Qty2

            tv_todayorderTitle.text = data.Review.Qty1Name
            tv_notAssQtyTitle.text = data.Review.Qty2Name

            if (data.AppMessage.AppMessageReadStatus > 0) {
                appNewMessageFlag.visibility = View.VISIBLE;
            } else {
                appNewMessageFlag.visibility = View.GONE;
            }
            if (data.MyFunction.NotRepairDoneQty > 0) {
                orderCountTextView.visibility = View.VISIBLE;
                orderCountTextView.text = data.MyFunction.NotRepairDoneQty.toString()
            } else {
                orderCountTextView.visibility = View.GONE;
            }
            if (data.MyFunction.QiangDanQty > 0) {
                robOrderCountTextView.visibility = View.VISIBLE;
                robOrderCountTextView.text = data.MyFunction.QiangDanQty.toString()
            } else {
                robOrderCountTextView.visibility = View.GONE;
            }
            if (!data.MyFunction.ShowQiangDan) {
                ll_rborder.visibility = View.GONE
            } else {
                ll_rborder.visibility = View.VISIBLE
            }
            if (data.MyFunction.TimeOutQty > 0) {
                timeOutCountTextView.visibility = View.VISIBLE;
                timeOutCountTextView.text = data.component1().TimeOutQty.toString()
            } else {
                timeOutCountTextView.visibility = View.GONE;
            }
        }

    }

    override fun createPresenter(): WorkOrderPresenter {
        return WorkOrderPresenter(this)
    }

    override fun onRefresh() {
        mPresenter.getData()
    }
}
